package com.demo8.basic.domain;

import com.demo8.basic.enums.BizCodeEnum;

import java.io.Serializable;

public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int SUCCESS;
    public static final String SUCCESS_MESSAGE;
    public static final int FAIL;
    public static final String FAIL_MESSAGE;
    private int code;
    private String msg;
    private boolean success;
    private T data;

    public static <T> CommonResult<T> ok() {
        return restResult( null, SUCCESS, SUCCESS_MESSAGE, true);
    }

    public static <T> CommonResult<T> ok(T data) {
        return restResult(data, SUCCESS, SUCCESS_MESSAGE, true);
    }

    public static <T> CommonResult<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg, true);
    }

    public static <T> CommonResult<T> fail() {
        return restResult( null, FAIL, FAIL_MESSAGE, false);
    }

    public static <T> CommonResult<T> fail(String msg) {
        return restResult(null, FAIL, msg, false);
    }

    public static <T> CommonResult<T> fail(BizCodeEnum bizCode) {
        return restResult(null, bizCode.getCode(), bizCode.getMsg(), false);
    }

    public static <T> CommonResult<T> fail(T data) {
        return restResult(data, FAIL, FAIL_MESSAGE, false);
    }

    public static <T> CommonResult<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg, false);
    }

    public static <T> CommonResult<T> fail(int code, String msg) {
        return restResult( null, code, msg, false);
    }

    private static <T> CommonResult<T> restResult(T data, int code, String msg, boolean success) {
        CommonResult<T> apiResult = new CommonResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setSuccess(success);
        return apiResult;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean getSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    static {
        SUCCESS = BizCodeEnum.SUCCESS.getCode();
        SUCCESS_MESSAGE = BizCodeEnum.SUCCESS.getMsg();
        FAIL = BizCodeEnum.BIZ_FAILED.getCode();
        FAIL_MESSAGE = BizCodeEnum.BIZ_FAILED.getMsg();
    }
}
